package com.zbkj.service.util;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.common.enums.BrandEnum;
import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.user.User;
import com.zbkj.common.utils.SecurityUtil;
import com.zbkj.common.vo.LoginUserVo;
import com.zbkj.service.service.BrandService;
import com.zbkj.service.service.MerchantService;
import com.zbkj.service.service.SystemAdminService;
import com.zbkj.service.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class BrandUtil {

    private static BrandUtil brandUtil;

    @Autowired
    private MerchantService merchantService;
    @Autowired
    private UserService userService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private SystemAdminService adminService;

    @Autowired
    private HttpServletRequest request;

    public static final String ZFB_URI = "zfb-front.xingtu-group.com";
    public static final String ZFB_URI_TEST = "zfb-test.brand.xingtu-group.com";


    @PostConstruct
    public void init(){
        brandUtil = this;
    }

    /**
     * 是否品牌
     * @return
     */
    public static boolean isBrand(){
        LoginUserVo userVo = SecurityUtil.getLoginUserVo();
        SystemAdmin user = userVo.getUser();
        log.info("用户信息:{}", user);
        boolean brand = !Objects.equals(0, user.getBrandId());
        if(brand){//是品牌,获取商户id集合
            List<Integer> list = new ArrayList<>();
            List<Merchant> mers = brandUtil.merchantService.list(new LambdaQueryWrapper<>(Merchant.class)
                    .eq(Merchant::getBrandId, getBrandId()).select(Merchant::getId)
            );
            if (mers != null){
                mers.forEach(item -> list.add(item.getId()));
            }
            if(list.size()==0){
                list.add(-1);
            }
            log.info("商户id集合:{}", list);
            user.setMerIds(list);
        }
        log.info("是否品牌:{}", brand);
        return brand;
    }

    /**
     * 获取品牌id
     * @return
     */
    public static Integer getBrandId(){
        return SecurityUtil.getLoginUserVo().getUser().getId();
    }

    /**
     * 获取品牌下商户id集合
     * @return
     */
    public static List<Integer> getMerchantIdList(){
        return SecurityUtil.getLoginUserVo().getUser().getMerIds();
    }

    public static List<Integer> getBrandUserIds() {
        List<Integer> ids = new ArrayList<>();
        LambdaQueryWrapper<User> q = new LambdaQueryWrapper<>();
        q.select(User::getId);
        q.eq(User::getBrandId, getBrandId());
        List<User> list = brandUtil.userService.list(q);
        if(list!=null){
            list.forEach(item -> ids.add(item.getId()));
        }
        return ids;
    }

    /**
     * 根据url获取品牌商id
     * @param request
     * @return
     */
    public static Integer getBrandIdByUrlOld(HttpServletRequest request) {
        String requestURI = String.valueOf(request.getRequestURL());
//        log.info("根据url获取品牌商idrequestURI==={}", requestURI);
        Integer brandId = getRealBrandId(requestURI);
        if (brandId == 0) {
            return 0;
        }
        SystemAdmin admin = brandUtil.adminService.getOne(new LambdaQueryWrapper<SystemAdmin>().select(SystemAdmin::getId).eq(SystemAdmin::getBrandId, brandId));
        if (ObjectUtil.isNotNull(admin)) {
            return admin.getId();
        }
        return 0;
    }

    /**
     * 根据url获取品牌商id
     * @param request
     * @return
     */
    public static Integer getBrandIdByUrl(HttpServletRequest request) {
        String requestURI = String.valueOf(request.getRequestURL());
        Integer brandId = brandUtil.brandService.getBrandId(requestURI);
//        log.info("根据url获取品牌商idrequestURI==={},brandId={}", requestURI, brandId);
        return brandId;
//        Integer brandId = getRealBrandId(requestURI);
//        if (brandId == 0) {
//            return 0;
//        }
//        SystemAdmin admin = brandUtil.adminService.getOne(new LambdaQueryWrapper<SystemAdmin>().select(SystemAdmin::getId).eq(SystemAdmin::getBrandId, brandId));
//        if (ObjectUtil.isNotNull(admin)) {
//            return admin.getId();
//        }
//        return 0;
    }

    public static Integer getBrandIdByUrlOld() {
        return getBrandIdByUrlOld(brandUtil.request);
    }

    public static Integer getRealBrandId(String requestURI) {
        return BrandEnum.getId(requestURI);
    }

    /**
     * 获取品牌商id
     * @return
     */
    public static Integer getBrandIdByUrl() {
        return getBrandIdByUrl(brandUtil.request);
    }


    public static boolean isZFBCR() {
        String requestURI = String.valueOf(brandUtil.request.getRequestURL());
        if (requestURI.contains(BrandEnum.ZFB.getFront()) || requestURI.contains(BrandEnum.ZFB.getAdmin())) {
            return true;
        }
        // 崇仁
        if (BrandEnum.isCR(requestURI)) {
            return true;
        }
        return false;
    }

    public static boolean isPuNingXuZhou() {
        String requestURI = String.valueOf(brandUtil.request.getRequestURL());
        // 普宁
        if (BrandEnum.isPN(requestURI)) {
            return true;
        }
        // 叙州
        if (BrandEnum.isXZ(requestURI)) {
            return true;
        }
        return false;
    }

    public static boolean isXuzhou() {
        String requestURI = String.valueOf(brandUtil.request.getRequestURL());
        // 叙州
        if (BrandEnum.isXZ(requestURI)) {
            return true;
        }
        return false;
    }

    public static String getBrandRedisPrefix() {
        Integer brandId = getBrandIdByUrl();
        if (brandId != null) {
            return "b:" + brandId + ":";
        }
        return "";
    }

}
